home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 242_01 / a51.h < prev    next >
Encoding:
Text File  |  1989-01-11  |  12.3 KB  |  430 lines

  1. /*
  2.     HEADER:        CUG242;
  3.     TITLE:        8051 Cross-Assembler (Portable);
  4.     FILENAME:    A51UTIL.C;
  5.     VERSION:    0.4;
  6.     DATE:        11/09/1988;
  7.  
  8.     DESCRIPTION:    "This program lets you use your computer to assemble
  9.             code for the Intel 8051 family of microprocessors.
  10.             The program is written in portable C rather than BDS
  11.             C.  All assembler features are supported except
  12.             relocation, linkage, and macros.";
  13.  
  14.     KEYWORDS:    Software Development, Assemblers, Cross-Assemblers,
  15.             Intel, 8051;
  16.  
  17.     SYSTEM:        CP/M-80, CP/M-86, HP-UX, MSDOS, PCDOS, QNIX;
  18.     COMPILERS:    Aztec C86, Aztec CII, CI-C86, Eco-C, Eco-C88, HP-UX,
  19.             Lattice C, Microsoft C,    QNIX C;
  20.  
  21.     WARNINGS:    "This program has compiled successfully on 2 UNIX
  22.             compilers, 5 MSDOS compilers, and 2 CP/M compilers.
  23.             A port to Toolworks C is untried."
  24.  
  25.     AUTHORS:    William C. Colley III;
  26. */
  27.  
  28. /*
  29.               8051 Cross-Assembler in Portable C
  30.  
  31.         Copyright (c) 1985,1987 William C. Colley, III
  32.  
  33. Revision History:
  34.  
  35. Ver    Date        Description
  36.  
  37. 0.0    JUL 1987    Adapted from version 0.0 of my portable 8048 cross-
  38.             assembler.  WCC3.
  39.  
  40. 0.1    OCT 1987    Changed the order of DW constants and made trailing
  41.             colons on labels get trashed.  WCC3.
  42.  
  43. 0.2    AUG 1988    Fixed bug that swapped the machine codes for CPL A and
  44.             CLR A.  WCC3.
  45.  
  46. 0.3    AUG 1988    Fixed a bug in the command line parser that puts it
  47.             into a VERY long loop if the user types a command line
  48.             like "A51 FILE.ASM -L".  WCC3 per Alex Cameron.
  49.  
  50. 0.4    NOV 1988    Fixed a bug that made DJNZ direct,relative generate
  51.             the wrong opcode.  WCC3.
  52.  
  53. This header file contains the global constants and data type definitions for
  54. all modules of the cross-assembler.  This also seems a good place to put the
  55. compilation and linkage instructions for the animal.  This list currently
  56. includes the following compilers:
  57.  
  58.         Compiler Name        Op. Sys.    Processor
  59.  
  60.     1)  Aztec C86            CP/M-86        8086, 8088
  61.                     MSDOS/PCDOS
  62.  
  63.     2)  AZTEC C II            CP/M-80        8080, Z-80
  64.  
  65.     3)  Computer Innovations C86    MSDOS/PCDOS    8086, 8088
  66.  
  67.     4)  Eco-C            CP/M-80        Z-80
  68.  
  69.     5)  Eco-C88            MSDOS/PCDOS    8086, 8088
  70.  
  71.     6)  HP C            HP-UX        68000
  72.  
  73.     7)  Lattice C            MSDOS/PCDOS    8086, 8088
  74.  
  75.     8)  Microsoft C            MSDOS/PCDOS    8086, 8088
  76.  
  77.     9)  QNIX C            QNIX        8086, 8088
  78.  
  79. Further additions will be made to the list as users feed the information to
  80. me.  This particularly applies to UNIX and IBM-PC compilers.
  81.  
  82. Compile-assemble-link instructions for this program under various compilers
  83. and operating systems:
  84.  
  85.     1)    Aztec C86:
  86.  
  87.     A)  Uncomment out the "#define AZTEC_C" line and comment out all
  88.         other compiler names in A51.H.
  89.  
  90.     B)  Assuming that all files are on drive A:, run the following sequence
  91.         of command lines:
  92.  
  93.         A>cc a51
  94.         A>cc a51eval
  95.         A>cc a51util
  96.         A>ln a51.o a51eval.o a51util.o -lc
  97.         A>era a51*.o
  98.  
  99.     2)  Aztec CII (version 1.06B):
  100.  
  101.     A)  Uncomment out the "#define AZTEC_C" line and comment out all
  102.         other compiler names in A51.H.
  103.  
  104.     B)  Assuming the C compiler is called "CC.COM" and all files are
  105.         on drive A:, run the following sequence of command lines:
  106.  
  107.         A>cc a51
  108.         A>as -zap a51
  109.         A>cc a51eval
  110.         A>as -zap a51eval
  111.         A>cc a51util
  112.         A>as -zap a51util
  113.         A>ln a51.o a51eval.o a51util.o -lc
  114.         A>era a51*.o
  115.  
  116.     3)  Computer Innovations C86:
  117.  
  118.     A)  Uncomment out the "#define CI_C86" line and comment out all
  119.         other compiler names in A51.H.
  120.  
  121.     B)  Compile the files A51.C, A51EVAL.C, and A51UTIL.C.  Link
  122.         according to instructions that come with the compiler.
  123.  
  124.     4)  Eco-C (CP/M-80 version 3.10):
  125.  
  126.     A)  Uncomment out the "#define ECO_C" line and comment out all
  127.         other compiler names in A51.H.
  128.  
  129.     B)  Assuming all files are on drive A:, run the following sequence of
  130.         command lines:
  131.  
  132.         A>cp a51 -i -m
  133.         A>cp a51eval -i -m
  134.         A>cp a51util -i -m
  135.         A>l80 a51,a51eval,a51util,a51/n/e
  136.         A>era a51*.mac
  137.         A>era a51*.rel
  138.  
  139.     5)  Eco-C88:
  140.  
  141.     A)  Uncomment out the "#define ECO_C" line and comment out all
  142.         other compiler names in A51.H.
  143.  
  144.     B)  Compile the files A51.C, A51EVAL.C, and A51UTIL.C.  Link
  145.         according to instructions that come with the compiler.
  146.  
  147.     6)  HP-UX (a UNIX look-alike running on an HP-9000 Series 200/500,
  148.     68000-based machine):
  149.  
  150.     A)  Uncomment out the "#define HP_UX" line and comment out all
  151.         other compiler names in A51.H.
  152.  
  153.     B)  Run the following command line:
  154.  
  155.         . cc a51.c a51eval.c a51util.c
  156.  
  157.     7)  Lattice C:
  158.  
  159.     A)  Uncomment out the "#define LATTICE_C" line and comment out all
  160.         other compiler names in A51.H.
  161.  
  162.     B)  Compile the files A51.C, A51EVAL.C, and A51UTIL.C.  Link
  163.         according to instructions that come with the compiler.
  164.  
  165.     8)  Microsoft C (version 3.00):
  166.  
  167.     A)  Uncomment out the "#define MICROSOFT_C" line and comment out all
  168.         other compiler names in A68.H.
  169.  
  170.     B)  Run the following command line:
  171.  
  172.         C>cl a51.c a51eval.c a51util.c
  173.  
  174.     9)    QNIX C:
  175.  
  176.     A)  Uncomment out the "#define QNIX" line and comment out all other
  177.         compiler names in A51.H.
  178.  
  179.     B)  Run the following command line:
  180.  
  181.         . cc a51.c a51eval.c a51util.c
  182.  
  183. Note that, under CP/M-80, you can't re-execute a core image from a previous
  184. assembly run with the "@.COM" trick.  This technique is incompatible with the
  185. Aztec CII compiler, so I didn't bother to support it at all.
  186. */
  187.  
  188. #include <stdio.h>
  189.  
  190. /*  Comment out all but the line containing the name of your compiler:    */
  191.  
  192. /* #define    AZTEC_C                            */
  193. /* #define    CI_C86                            */
  194. /* #define    ECO_C                            */
  195. /* #define    HP_UX                            */
  196. /* #define    LATTICE_C                        */
  197. #define        MICROSOFT_C
  198. /* #define    QNIX                            */
  199.  
  200. /*  Compiler dependencies:                        */
  201.  
  202. #ifdef    AZTEC_C
  203. #define    getc(f)        agetc(f)
  204. #define    putc(c,f)    aputc(c,f)
  205. #endif
  206.  
  207. #ifndef    ECO_C
  208. #define    FALSE        0
  209. #define    TRUE        (!0)
  210. #endif
  211.  
  212. #ifdef    LATTICE_C
  213. #define    void        int
  214. #endif
  215.  
  216. #ifdef    QNIX
  217. #define    fprintf        tfprintf
  218. #define    printf        tprintf
  219. #endif
  220.  
  221. /*  On 8-bit machines, the static type is as efficient as the register    */
  222. /*  type and far more efficient than the auto type.  On larger machines    */
  223. /*  such as the 8086 family, this is not necessarily the case.  To    */
  224. /*  let you experiment to see what generates the fastest, smallest code    */
  225. /*  for your machine, I have declared internal scratch variables in    */
  226. /*  functions "SCRATCH int", "SCRATCH unsigned", etc.  A SCRATCH    */
  227. /*  varible is made static below, but you might want to try register    */
  228. /*  instead.                                */
  229.  
  230. #define    SCRATCH        static
  231.  
  232. /*  A slow, but portable way of cracking an unsigned into its various    */
  233. /*  component parts:                            */
  234.  
  235. #define    clamp(u)    ((u) &= 0xffff)
  236. #define    high(u)        (((u) >> 8) & 0xff)
  237. #define    low(u)        ((u) & 0xff)
  238. #define    word(u)        ((u) & 0xffff)
  239.  
  240. /*  The longest source line the assembler can hold without exploding:    */
  241.  
  242. #define    MAXLINE        255
  243.  
  244. /*  The maximum number of source files that can be open simultaneously:    */
  245.  
  246. #define    FILES        4
  247.  
  248. /*  The fatal error messages generated by the assembler:        */
  249.  
  250. #define    ASMOPEN        "Source File Did Not Open"
  251. #define    ASMREAD        "Error Reading Source File"
  252. #define    DSKFULL        "Disk or Directory Full"
  253. #define    FLOFLOW        "File Stack Overflow"
  254. #define    HEXOPEN        "Object File Did Not Open"
  255. #define    IFOFLOW        "If Stack Overflow"
  256. #define    LSTOPEN        "Listing File Did Not Open"
  257. #define    NOASM        "No Source File Specified"
  258. #define    SYMBOLS        "Too Many Symbols"
  259.  
  260. /*  The warning messages generated by the assembler:            */
  261.  
  262. #define    BADOPT        "Illegal Option Ignored"
  263. #define    NOHEX        "-o Option Ignored -- No File Name"
  264. #define    NOLST        "-l Option Ignored -- No File Name"
  265. #define    TWOASM        "Extra Source File Ignored"
  266. #define    TWOHEX        "Extra Object File Ignored"
  267. #define    TWOLST        "Extra Listing File Ignored"
  268.  
  269. /*  Line assembler (A51.C) constants:                    */
  270.  
  271. #define    BIGINST        3        /* longest instruction length    */
  272. #define    IFDEPTH        16        /* maximum IF nesting level    */
  273. #define    NOP        0x00        /* processor's NOP opcode    */
  274. #define    ON        1        /* assembly turned on        */
  275. #define    OFF        -1        /* assembly turned off        */
  276.  
  277. /*  Line assembler (A51.C) opcode attribute word flag masks:        */
  278.  
  279. #define    PSEUDO        0x8000        /* is pseudo-op            */
  280. #define    ISIF        0x4000        /* is IF, ELSE, or ENDI        */
  281. #define    OTYPE        0x3e00        /* opcode type:            */
  282. #define    MOV        0x0000        /*    MOV            */
  283. #define    MOVX        0x0200        /*    MOVX            */
  284. #define    ORL        0x0400        /*    ORL, ANL        */
  285. #define    XRL        0x0600        /*    XRL            */
  286. #define    ADD        0x0800        /*    ADD, ADDC, SUBB        */
  287. #define    DJNZ        0x0a00        /*    DJNZ            */
  288. #define    CJNE        0x0c00        /*    CJNE            */
  289. #define    XCH        0x0e00        /*    XCH            */
  290. #define    INC        0x1000        /*    INC            */
  291. #define    DEC        0x1200        /*    DEC            */
  292. #define    POP        0x1400        /*    POP, PUSH        */
  293. #define    JBIT        0x1600        /*    JBC, JB, JNB        */
  294. #define    SJMP        0x1800        /*    SJMP, JC, JNC, JZ, ...    */
  295. #define    AJMP        0x1a00        /*    AJMP, ACALL        */
  296. #define    LJMP        0x1c00        /*    LJMP, LCALL        */
  297. #define    CPL        0x1e00        /*    CLR, CPL        */
  298. #define    SETB        0x2000        /*    SETB            */
  299. #define    MOVC        0x2200        /*    MOVC            */
  300. #define    JMP        0x2400        /*    JMP            */
  301. #define    XCHD        0x2600        /*    XCHD            */
  302. #define    SWAP        0x2800        /*    SWAP, RL, RLC, MUL, ...    */
  303. #define    RET        0x2c00        /*    NOP, RET, RETI        */
  304. #define    RARGS        0x01e0        /* register argument range    */
  305. #define    RARG        0x001f        /* lowest register argument    */
  306.  
  307. /*  Line assembler (A51.C) pseudo-op opcode token values:        */
  308.  
  309. #define    BIT        0
  310. #define    DB        1
  311. #define    DS        2
  312. #define    DW        3
  313. #define    ELSE        4
  314. #define    END        5
  315. #define    ENDIF        6
  316. #define    EQU        7
  317. #define    IF        8
  318. #define    INCL        9
  319. #define    ORG        10
  320. #define    REGI        11
  321. #define    PAGE        12
  322. #define    SET        13
  323. #define    TITL        14
  324.  
  325. /*  Lexical analyzer (A51EVAL.C) token buffer and stream pointer:    */
  326.  
  327. typedef struct {
  328.     unsigned attr;
  329.     unsigned valu;
  330.     char sval[MAXLINE + 1];
  331. } TOKEN;
  332.  
  333. /*  Lexical analyzer (A51EVAL.C) token attribute values:        */
  334.  
  335. #define    EOL        0    /*  end of line                */
  336. #define    SEP        1    /*  field separator            */
  337. #define    OPR        2    /*  operator                */
  338. #define    STR        3    /*  character string            */
  339. #define    VAL        4    /*  value                */
  340. #define    REG        5    /*  register or other tag        */
  341. #define    IMM        6    /*  immediate mode flag (#)        */
  342. #define    DOT        7    /*  bit address operator (.)        */
  343. #define    BVAL        9    /*  bit address value            */
  344.  
  345. /*  Lexical analyzer (A51EVAL.C) token attribute word flag masks:    */
  346.  
  347. #define    BINARY        0x8000    /*  Operator:    is binary operator    */
  348. #define    UNARY        0x4000    /*        is unary operator    */
  349. #define    PREC        0x0f00    /*        precedence        */
  350.  
  351. #define    FORWD        0x8000    /*  Value:    is forward referenced    */
  352. #define    SOFT        0x4000    /*        is redefinable        */
  353.  
  354. #define    TYPE        0x000f    /*  All:    token type        */
  355.  
  356. /*  Lexical analyzer (A51EVAL.C) register token values:            */
  357. #define    A        0x00    /*  A                    */
  358. #define    AT_R0        0x02    /*  @R0                    */
  359. #define    AT_R1        0x03    /*  @R1                    */
  360. #define    R0        0x04    /*  R0                    */
  361. #define    R1        0x05    /*  R1                    */
  362. #define    R2        0x06    /*  R2                    */
  363. #define    R3        0x07    /*  R3                    */
  364. #define    R4        0x08    /*  R4                    */
  365. #define    R5        0x09    /*  R5                    */
  366. #define    R6        0x0a    /*  R6                    */
  367. #define    R7        0x0b    /*  R7                    */
  368. #define    AT_DPTR        0x0c    /*  @DPTR                */
  369. #define    PC        0x0d    /*  PC                    */
  370. #define    DPTR        0x0e    /*  DPTR                */
  371. #define    AT_A        0x0f    /*  @A                    */
  372. #define    AB        0x10    /*  AB                    */
  373. #define    C        0x11    /*  C                    */
  374.  
  375. /*  Lexical analyzer (A51EVAL.C) operator token values (unlisted ones    */
  376. /*  use ASCII characters):                        */
  377.  
  378. #define    AND        0
  379. #define    GE        1
  380. #define    HIGH        2
  381. #define    LE        3
  382. #define    LOW        4
  383. #define    MOD        5
  384. #define    NE        6
  385. #define    NOT        7
  386. #define    OR        8
  387. #define    SHR        9
  388. #define    SHL        10
  389. #define    XOR        11
  390.  
  391. /*  Lexical analyzer (A51EVAL.C) operator precedence values:        */
  392.  
  393. #define    UOP1        0x0000    /*  unary +, unary -            */
  394. #define    MULT        0x0100    /*  *, /, MOD, SHL, SHR            */
  395. #define    ADDIT        0x0200    /*  binary +, binary -            */
  396. #define    RELAT        0x0300    /*  >, >=, =, <=, <, <>            */
  397. #define    UOP2        0x0400    /*  NOT                    */
  398. #define    LOG1        0x0500    /*  AND                    */
  399. #define    LOG2        0x0600    /*  OR, XOR                */
  400. #define    UOP3        0x0700    /*  HIGH, LOW                */
  401. #define    RPREN        0x0800    /*  )                    */
  402. #define    LPREN        0x0900    /*  (                    */
  403. #define    ENDEX        0x0a00    /*  end of expression            */
  404. #define    START        0x0b00    /*  beginning of expression        */
  405.  
  406. /*  Utility package (A51UTIL.C) symbol table routines:            */
  407.  
  408. struct _symbol {
  409.     unsigned attr;
  410.     unsigned valu;
  411.     struct _symbol *left, *right;
  412.     char sname[1];
  413. };
  414.  
  415. typedef struct _symbol SYMBOL;
  416.  
  417. #define    SYMCOLS        4
  418.  
  419. /*  Utility package (A51UTIL.C) opcode/operator table routines:        */
  420.  
  421. typedef struct {
  422.     unsigned attr;
  423.     unsigned valu;
  424.     char oname[7];
  425. } OPCODE;
  426.  
  427. /*  Utility package (A51UTIL.C) hex file output routines:        */
  428.  
  429. #define    HEXSIZE        32
  430. #define    UOP1        0x0000    /*